// pages/appointment/index.js
const { appointment, wxPayment, getOpenId } = require('../../api/api.js')
const { formatTime } = require('../../utils/util.js')
const currentDate = new Date()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTitle: {
      top: 0,
      height: 0,
    },
    checkDate: '',
    checkDateFormat: '',
    dateTimePopupShow: false,
    dateTimePickerFormatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    minDate: currentDate.getTime(),
    maxDate: currentDate.setFullYear(currentDate.getFullYear() + 2),
    // typeSelectIndex: -1,
    // typeRange: ['精装', '毛坯',],
    address: '',
    area: '',
    name: '',
    phone: '',
    showDialog:false,
    houseTypeId:0,
    isHardcover:''
  },

  goBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  openDialog(){
    this.setData({
      showDialog:true
    }) 
  },
  toggleDialog(){
    this.setData({
      showDialog:!this.data.showDialog
    })
  },
  handleDateTimePickerTap() {
    this.setData({
      dateTimePopupShow: true,
    })
  },
  handleDateTimePickerConfirm(value) {
    this.setData({
      dateTimePopupShow: false,
      checkDate: value.detail,
      checkDateFormat: formatTime(new Date(value.detail))
    })
  },
  handleDateTimePickerCancel() {
    this.setData({
      dateTimePopupShow: false,
    })
  },
  // handleTypePickerChange(e) {
  //   this.setData({
  //     typeSelectIndex: e.detail.value
  //   })
  // },
  tapHardCover(){
    this.setData({
      isHardcover:true,
      houseTypeId:1
    })
  },
  tapNoCover(){
    this.setData({
      isHardcover:false,
      houseTypeId:2
    })
  },
  chooseLocation() {
    const that = this
    wx.chooseLocation({
      success(res) {
        const { name, address, latitude, longitude } = res;
        console.log(name, address, latitude, longitude)
        that.setData({
          address:address+name
        })
      },
      fail(res) {

      }
    })
  },
  async appointmentSubmit() {
    const { checkDate, checkDateFormat, address, area, typeSelectIndex, typeRange, name, phone,houseTypeId } = this.data
    // const selectHouseType = typeRange[typeSelectIndex]
    if (!checkDateFormat) {
      wx.showToast({
        title: '请输入验房时间',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!address) {
      wx.showToast({
        title: '请选择验房小区位置',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!area) {
      wx.showToast({
        title: '请输入房屋建筑面积',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (houseTypeId==0) {
      wx.showToast({
        title: '请选择验房类型',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!name) {
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!phone) {
      wx.showToast({
        title: '请输入您的电话',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (phone.length < 11) {
      wx.showToast({
        title: '请输入11位的手机号码',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.login({
      success: async (res) => {
        let code = res.code
        const { data: { openid: openId } } = await getOpenId({ code })
        const amount =100;
        const payResult = await wxPayment({
          amout: amount,
          openId
        })
        wx.requestPayment({
          ...payResult.data,
          async success(res) {
            const result = await appointment({
              deposit: amount,
              houseArea: area,
              houseName: address,
              houseTypeId: houseTypeId,
              inspectDate: checkDateFormat,
              preBookType:2,
              name,
              phone
            })
            wx.showToast({
              title: '预约成功',
              icon: 'success',
              duration: 1000,
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 1000)
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect();
    this.setData({
      topTitle: {
        top,
        height
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '家晓验房＠品牌验房【全国连锁更专业】',
      path: '/pages/index/index',
      imageUrl: '/assets/image/share-img.png'
    }
  }
})